package com.tycmc.signelectroniccontracts.tools.update;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.RemoteViews;

import androidx.core.content.FileProvider;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;
import com.tycmc.signelectroniccontracts.tools.show.ToastShow;

import java.io.File;
import java.util.Map;

/**
 * Created by GY on 2018/1/21.
 */

public class DownloadTool {


    private static NotificationManager notificationManager;
    private static Notification notification;


    public static void upData(final ActBase actBase, String urlPath) {
        final DownLoadProgerss progerss = new DownLoadProgerss(actBase);
        progerss.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = v.getId();
                if (i == R.id.bt_cancel) {
                    NetDown.cancelDown();

                } else if (i == R.id.bt_backdown) {
                    createNotification(actBase);
                }
            }
        });
        progerss.show();
        NetDown.downLoadApk(urlPath, new DownloadInterface() {
            @Override
            public void state(int state, Object o) {
                switch (state) {
                    case NetDown.SUCECC:
                        clean();
                        if (o instanceof File) {
                            installApk(actBase, ((File) o));
                        }
                        progerss.dimiss();
                        break;
                    case NetDown.UPDATAING:
                        if (o instanceof Map) {
                            Map<String, Integer> map = (Map) o;
                            if (notificationManager != null) {
                                upData(actBase, (int) (map.get(NetDown.SIZE) * 100.0 / map.get(NetDown.MAX)));
                            } else {
                                progerss.setPro(map.get(NetDown.MAX), map.get(NetDown.SIZE));
                            }
                        }
                        break;
                    case NetDown.CANCEL:
                        clean();
                        ToastShow.popupToast(actBase, "取消下载");
                        progerss.dimiss();
                        break;
                    case NetDown.FAIL:
                        clean();
                        LogShow.i("下载失败", o.toString());
                        ToastShow.popupToast(actBase, "下载失败");
                        progerss.dimiss();
                        break;
                }
            }
        });
    }

    //安装apk
    protected static void installApk(final ActBase context, final File file) {
        //安装apk时需要判断是否需要增加权限判断
        PermissionsTool.requestPermiss(context, new PermissionsTool.PerMissionCallBack() {
            @Override
            public void permissIsPass(String[] permission, boolean isPass) {
                if (isPass) {
                    Intent intent = new Intent();
                    //执行动作
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //执行的数据类型
                    if (android.os.Build.VERSION.SDK_INT < 24) {
                        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    } else {
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.setDataAndType(FileProvider.getUriForFile(context, context.getPackageName(), file), "application/vnd.android.package-archive");
                    }
                    context.startActivity(intent);
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }
        });

    }


    public static void clean() {
        if (notificationManager != null) {

            notificationManager.cancelAll();
        }
        notification = null;
        notificationManager = null;
    }

    public static void createNotification(ActBase act) {

        notificationManager = (NotificationManager) act.getSystemService(Context.NOTIFICATION_SERVICE);
        notification = new Notification();
        notification.icon = R.drawable.appicon;
        // 这个参数是通知提示闪出来的值.

        /***
         * 在这里我们用自定的view来显示Notification
         */
        upData(act, 0);
    }

    private static void upData(ActBase act, int size) {
        RemoteViews contentView = new RemoteViews(act.getPackageName(), R.layout.layout_notifition_layout);
        notification.contentView = contentView;

        contentView.setProgressBar(R.id.progress_dialog, 100, size, false);

        contentView.setImageViewResource(R.id.iv_icon, R.drawable.appicon);//通过RemoteViews来设置Notification中的图片
        contentView.setTextViewText(R.id.tv_download_title, "正在下载");//通过RemoteViews来设置Notification中的TextView

        notificationManager.notify(R.string.app_name, notification);
    }



}
